import Mock from "mockjs";
import dayjs from "dayjs";
import { param2Obj } from "../utls/utilsDemo";
let List = [];
for (let i = 0; i < 200; i++) {
  List.push({
    id: "000" + i,
    num: Mock.mock("@integer(0, 7)"),
    title: "幼儿英语入门课程",
    type: Mock.mock("@integer(0, 1)"),
    money: Mock.mock("@integer(100, 1000)"),
    sales: Mock.mock("@integer(100, 1000)"),
    name: Mock.mock("@first"),
    time: Mock.Random.date("yyyy-MM-dd"),
  });
}
export default {
  getCourseData: (config) => {
    console.log(param2Obj(config.url));
    let { keyword, page, pagesize } = param2Obj(config.url);
    let newarr = List.filter((item) => {
      return item.id.indexOf(keyword) != -1;
    });
    let pageReset = newarr.filter((item, index) => {
      return (page - 1) * pagesize <= index && index < page * pagesize;
    });
    return {
      code: 200,
      msg: "请求成功",
      data: pageReset,
      total: List.length,
    };
  },
  deleteData: (config) => {
    let id = JSON.parse(config.body).id;
    List.forEach((item, index) => {
      if (item.id == id) {
        List.splice(index, 1);
      }
    });
    return {
      code: 200,
      msg: "删除成功",
      data: null,
    };
  },
  batchDeleteData: (config) => {
    console.log(config.body);
    var { ids } = JSON.parse(config.body);
    console.log("ids", ids);
    var newArr = List.filter((item) => ids.indexOf(item.id) == -1);
    List = newArr;
    return {
      code: 200,
      msg: "批量删除成功",
      data: null,
    };
  },
  updateData: (config) => {
    console.log("config", config);
    var { id, title, type, num, money, sales, name, time } = JSON.parse(
      config.body
    );
    List.forEach((item) => {
      if (item.id == id) {
        // item就是我们查找的旧的值
        item.id = id;
        item.title = title;
        item.type = type;
        item.num = num;
        item.money = money;
        item.sales = sales;
        item.name = name;
        item.time = dayjs(time).format("YYYY-MM-DD");
      }
    });
    return {
      code: 200,
      msg: "修改成功",
      data: null,
    };
  },
};
